<template>
  <div>
    <a-modal v-model="LVisible" destroyOnClose :maskClosable="false" :title="title" @ok="handleOk"
      :confirm-loading="confirmLoading">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 4 }"
        :wrapper-col="{ span: 20 }">
        <a-form-model-item ref="parentName" label="所属字典" prop="parentName">
          <a-input v-model="form.parentName" disabled />
        </a-form-model-item>
        <a-form-model-item ref="name" label="字典名称" prop="name">
          <a-input v-model="form.name" />
        </a-form-model-item>
        <a-form-model-item ref="value" label="字典值" prop="value">
          <a-input v-model="form.value" />
        </a-form-model-item>
        <a-form-model-item ref="sortNo" label="排序" prop="sortNo">
          <a-input-number step="1" :min="0" v-model="form.sortNo" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import { validatorFunction } from './dict'
export default {
  name: 'DictModal',
  mixins: [validatorFunction],
  props: ['url'],
  data () {
    return {
      LVisible: false,
      confirmLoading: false,
      title: '新增字典',
      disableSubmit: true,
      form: {},
      rules: {
        value: [
          { required: true, message: '请输入字典值', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请填写字典名称', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
  },
  methods: {
    open (parent, record) {
      this.LVisible = true
      if (!record) {
        this.title = '新增字典'
        this.form = {
          parentId: parent.id,
          parentName: parent.name,
          dictType: '02'
        }
      } else {
        this.title = '修改字典'
        this.form = {
          parentId: parent.id,
          parentName: parent.name,
          id: record.id,
          value: record.value,
          name: record.name,
          sortNo: record.sortNo,
          dictType: record.dictType
        }
      }
    },
    handleOk () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          let url = ''
          if (!this.form.id) {
            url = this.url.add
          } else {
            url = this.url.edit
          }
          this.$post(url, this.form).then(res => {
            this.confirmLoading = false
            if (this.isSuccess(res)) {
              this.$message.success('操作成功')
              this.LVisible = false
              this.$emit('LModalOk')
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
